package com.mustafaiev.tair.cts.tapestry.pages.baskets;

import java.util.List;

import org.apache.tapestry5.annotations.InjectPage;
import org.apache.tapestry5.annotations.OnEvent;
import org.apache.tapestry5.annotations.Persist;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.annotations.SetupRender;
import org.apache.tapestry5.beaneditor.BeanModel;
import org.apache.tapestry5.ioc.Messages;
import org.apache.tapestry5.ioc.annotations.Inject;
import org.apache.tapestry5.services.BeanModelSource;

import com.mustafaiev.tair.cts.facade.IBasketFacade;
import com.mustafaiev.tair.cts.facade.IPurchaseFacade;
import com.mustafaiev.tair.cts.factory.ICostTrackingSystemFacadeFactory;
import com.mustafaiev.tair.cts.tapestry.dto.BasketDTO;
import com.mustafaiev.tair.cts.tapestry.dto.PurchaseDTO;
import com.mustafaiev.tair.cts.util.CtsUtil;

public class View {

	@Inject
	private ICostTrackingSystemFacadeFactory facadeFactory;

	@InjectPage
	private Index basketsPage;

	@InjectPage
	private Edit editPage;

	@Persist
	private String costs;

	@Property
	@Persist
	private BasketDTO basket;

	@Property
	@Persist
	private List<PurchaseDTO> purchases;

	@Property
	private PurchaseDTO purchase;

	@Property
	@Persist
	private String basketId;

	@Property
	private BeanModel<PurchaseDTO> purchaseGridModel;

	@Inject
	private BeanModelSource beanModelSource;

	@Inject
	private Messages messages;

	String onPassivate() {
		return this.basketId;
	}

	void onActivate(final String basketId) {
		this.basketId = basketId;
	}

	public String getDate() {
		return CtsUtil.formatOutputDate(this.basket.getDate());
	}

	public String getFirstname() {
		return this.basket.getPayerFirstname();
	}

	public String getLastname() {
		return this.basket.getPayerLastname();
	}

	public String getCosts() {
		return this.costs;
	}

	@SetupRender
	void setupRender() {
		this.purchaseGridModel = this.beanModelSource.createDisplayModel(
				PurchaseDTO.class, this.messages);

		this.purchaseGridModel.add("Owner").sortable(true);

		this.basket = getBasketFacade().retrieveBasket(
				Long.valueOf(this.basketId));
		this.purchases = getPurchaseFacade().retrievePurchasesForBasket(
				Long.valueOf(this.basketId));
		this.costs = getPurchasesCosts();
	}

	@OnEvent(value = "back")
	Object goBack() {
		return this.basketsPage;
	}

	@OnEvent(value = "edit")
	Object goEdit() {
		this.editPage.init(this.basket, this.purchases);
		return this.editPage;
	}

	private IBasketFacade getBasketFacade() {
		final IBasketFacade facade = this.facadeFactory
				.getFacade(IBasketFacade.class);
		return facade;
	}

	private IPurchaseFacade getPurchaseFacade() {
		final IPurchaseFacade facade = this.facadeFactory
				.getFacade(IPurchaseFacade.class);
		return facade;
	}

	private String getPurchasesCosts() {
		double purchasesCost = 0d;
		if (this.purchases != null) {
			for (final PurchaseDTO purchase : this.purchases) {
				purchasesCost = purchasesCost + purchase.getCost();
			}
		}
		return String.valueOf(purchasesCost);
	}

}
